// 日期区间
laydate.render({
    elem: '#between'
    ,range: true
  });
//  公司列表
  $('.company_name').on('click',function(){
    opsearch(path.company.index+'?op=search')
})

// 添加编制跳转
$('#add').on('click',()=>{
    window.location.href = "{:url('html/car/car_compile_add')}"
})

var page = 1;
var pagenum = 10;
var fi=1;
var url={
    list:api.compile.list,
    delete:api.compile.delete,
}
var companyId= '{$Think.get.policy_type}';
var strengthType= '{$Think.get.strength_type}';
var State= '{$Think.get.state}';
var data={};
data.page=page;
data.pagenum=pagenum;
var where_json ={};


var main=[[ //表头
    {type:'checkbox', fixed: 'left'}
//    ,{field: 'id', title: '编制号', fixed: 'left'}
   ,{field:'state', title:'审核状态',templet:"#State"}
   ,{field: 'strength_type', title: '编制车型',templet:"#strength",mimWidth:130}
   ,{field: 'company', title: '公司',templet:'<div>{{d.company.company_name?d.company.company_name:"未绑定公司"}}</div>',minWidth:200}
   ,{field: 'budget_early', title: '年初预算'}
   ,{field: 'budget_add', title: '年中追加预算',minWidth:130}
   ,{field: 'standard', title: '车辆类型',templet:'#carType'}  
   ,{field:'standard', title:'排量',templet:'<div>{{JSON.parse(d.standard).displacement?JSON.parse(d.standard).displacement:"-"}}</div>'}
//    ,{field:'policy_charge', title:'用途'}
    // ,{field:'audit_time', title:'编制时间'}
    ,{field:'policy_charge', title:'是否新能源',sort:true,templet:'<div>{{JSON.parse(d.standard).new_energy==1?"是":"否"}}</div>',minWidth:110}
    ,{field:'budget_source', title:'资金来源'}
   ,{field:'reason', title:'审核原因',templet:'<div>{{d.reason?d.reason:"-"}}</div>'}
   ,{ title: '操作',minWidth:200, toolbar:'#demo',fixed: 'right',align:"center"}
]];

if(companyId){
    where_json.company_id=['in',companyId]
}
if(strengthType){
    where_json.strength_type=['in',strengthType]
}
if(State){
    where_json.state=['in',State]
}

data.where_json=JSON.stringify(where_json)

ajaxsevice(data);
function ajaxsevice(data) {
    Load();
    //请求数据
    ajax(url.list, data, 'post').done(function (res) {
        console.log(res)
        Load(1);
        // 成功回调
        //方法级表单渲染
        table.render({
            elem: '#insurance',
            cols: main,
            data: res.data,
            limit: pagenum,
            cellMinWidth: 100
        });
        if(fi==1){
            laypage.render({
                elem: 'page'
                ,count: res.count
                ,layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
                ,theme: '#1E9FFF'
                ,jump: function(obj, first){
                    console.log(obj)
                    if(!first){
                        page=obj.curr;
                        data.page=page;
                        pagenum=obj.limit;
                        data.pagenum=pagenum;
                        ajaxsevice(data);
                    }else{
                        
                    }
                }
              })
              fi = 2;
        }
    
        table.on('tool(demo)', function(obj){
            var data1 = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var data2 = {
                ids:data1.id
            };
            if(layEvent === 'edit'){ 
                window.location.href = "{:url('html/car/car_compile_add')}?id="+data1.id;
            }else if(layEvent==='del'){
                layer.confirm('是否删除', function (index) {
                    Load()
                    ajax(url.delete, data2, 'post').done(function (res) {
                        // 成功回调
                        // console.log(res)
                        layer.closeAll();
                        Msg('删除成功');
                        ajaxsevice(data)
                    }).fail(function (err) {
                        // 失败回调
                        layer.close(deleteload);
                        Err(res.msg ? res.msg : '操作失败，稍候再试');
                    });
                    layer.close(index);
                })
            }else if (layEvent==='auditNew'){
                window.location.href = "{:url('html/car/car_compile_audit')}?id="+data1.id+"&state=1";
            }else if (layEvent==='auditUpdata'){
                window.location.href = "{:url('html/car/car_compile_audit')}?id="+data1.id+"&state=4";
            }else if(layEvent==='bind'){
                window.location.href = "{:url('html/car/car_compile_bind')}?id="+data1.id;
            }else if(layEvent==='updata'){
                window.location.href = "{:url('html/car/car_compile_updata')}?id="+data1.id+"&state=6";

            }else if(layEvent==='approve'){
                window.location.href = "{:url('html/car/car_compile_updata')}?id="+data1.id+"&state=5";
            }else if(layEvent==='progress'){
                window.location.href = "{:url('html/car/car_compile_progress')}?id="+data1.id;
            }
        })
          			//删除多个
    			$(".delete-multiple").on('click', function () {
    				var ids = '';
                    var checkStatus = table.checkStatus('insurance'); //test即为基础参数id对应的值
                    // console.log('多选信息')
                    // console.log(checkStatus)
    				// console.log(checkStatus.data) //获取选中行的数据
    				// console.log(checkStatus.data.length) //获取选中行数量，可作为是否有选中行的条件
                    // console.log(checkStatus.isAll) //表格是否全选
                    if(checkStatus.data.length){
    				for (var dei = 0; dei < checkStatus.data.length; dei++) {
    					if (dei == 0) {
    						ids = checkStatus.data[dei].id
    					} else {
    						ids += ("," + checkStatus.data[dei].id)
    					}
                    }
                   
                    var data2 ={
                        ids : ids
                    }
    				layer.open({
    					type: 1,
    					title: false,
    					id: "delete",
    					content: '<div style="padding: 20px 100px;">是否删除</div>',
    					btn: ['确定', '取消'],
    					btnAlign: 'c' //按钮居中
    						,
    					shade: 0 //不显示遮罩
    						,
    					yes: function () {
    						var deleteload = layer.load(1, {
    							shade: [0.1, '#fff'] //0.1透明度的白色背景
                            });
                            console.log(data2)
                            ajax(url.delete,data2,'post').done(function(res) {
                                // 成功回调
                            			layer.close(deleteload);
                                        layer.closeAll();
                                        Msg('删除成功');
                                        ajaxsevice(data)
                                }).fail(function(err) {
                                // 失败回调
                                layer.close(deleteload);
    						    Err(res.msg ? res.msg : '操作失败，稍候再试');
                                });
    					},
    					btn2: function () {
    						layer.closeAll();
    					}
                    });
                }else{
                    Err('无选择项')
                }
    			});
    }).fail(function (err) {
        // 失败回调
        Load(1)
    	Err('请求失败')
    });

};